<template>
  <!-- Excel 导入功能组件 -->
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEmployeeAPI } from '@/api'
import { formatExcelDate } from '@/utils'
export default {
  name: 'Excel',
  methods: {
    // 导入成功以后的回调函数
    async handleSuccess({ results, header }) {
      const obj = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      const newArr = []
      results.forEach(item => {
        const newObj = {}
        const keysArr = Object.keys(item)
        keysArr.forEach(key => {
          const enKey = obj[key]
          if (enKey === 'correctionTime' || enKey === 'timeOfEntry') {
            const value = formatExcelDate(item[key])
            newObj[enKey] = value
          } else {
            newObj[enKey] = item[key]
          }
        })
        newArr.push(newObj)
      })
      console.log(newArr)
      const res = await importEmployeeAPI(newArr).catch(err => err)
      if (res.success) this.$message.success(res.message)
      this.$router.back()
      this.$message.success('操作成功')
    }
  }
}
</script>

<style lang="scss" scoped></style>
